package com.ruoyi.project.tool.swagger.WorkerController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.interceptor.annotation.RepeatSubmit;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysServiceOrder;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysServiceOrderService;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 用户服务订单Controller
 *
 * @author jjp
 * @date 2020-04-16
 */
@RestController
@RequestMapping("/mobileWorker/order")
public class WorkServiceOrderController extends BaseController {
    @Autowired
    private ISysServiceOrderService sysServiceOrderService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询用户服务订单列表
     */
    @GetMapping("/list")
    @RepeatSubmit
    @SignatureValidation
    public TableDataInfo list(SysServiceOrder sysServiceOrder) {
        String username = SecurityUtils.getUsername();
        SysUser sysUser = sysUserService.selectUserByUserName(username);
        Long userId = sysUser.getUserId();
        sysServiceOrder.setUserId(userId);
        startPage();
        List<SysServiceOrder> list = sysServiceOrderService.selectSysServiceOrderList(sysServiceOrder);
        return getDataTable(list);
    }


    /**
     * 获取用户服务订单详细信息
     */
    @GetMapping(value = "/{orderId}")
    @RepeatSubmit
    @SignatureValidation
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(sysServiceOrderService.selectSysServiceOrderById(orderId));
    }

    /**
     * 新增用户服务订单
     */
    @Log(title = "用户服务订单", businessType = BusinessType.INSERT)
//    @PostMapping
    public AjaxResult add(@RequestBody SysServiceOrder sysServiceOrder) {
        return toAjax(sysServiceOrderService.insertSysServiceOrder(sysServiceOrder));
    }

    /**
     * 修改用户服务订单
     */
    @Log(title = "用户服务订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @SignatureValidation
    @PutMapping
    public AjaxResult edit(@RequestBody SysServiceOrder sysServiceOrder) {
        SysUser sysUser = sysUserService.selectUserByUserName(SecurityUtils.getUsername());
        Long userId = sysUser.getUserId();
        sysServiceOrder.setUserId(userId);
        //修改订单状态为进行中
        sysServiceOrder.setOrderStatus("2");
        return toAjax(sysServiceOrderService.updateSysServiceOrder(sysServiceOrder));
    }

    /**
     * 删除用户服务订单
     */
    @Log(title = "用户服务订单", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(sysServiceOrderService.deleteSysServiceOrderByIds(orderIds));
    }
}
